package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.dubbo.api.BlossomApi;
import com.tanhua.dubbo.api.JsrApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.Fsr;
import com.tanhua.model.domain.Jsr;
import com.tanhua.model.domain.UserInfo;

import com.tanhua.model.vo.ErrorResult;

import com.tanhua.model.vo.PeachVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.Date;
import java.util.List;

@Service
public class BlossomService {
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;
    @DubboReference
    private BlossomApi blossomApi;
    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private JsrApi jsrApi;



    public void send(MultipartFile soundFile) throws IOException {
        if (soundFile.isEmpty()){
            throw new BusinessException(ErrorResult.error());
        }
        //1.将语音上传到FastDfs,获取访问的url
        String filename = soundFile.getOriginalFilename();//abc.mp4
        filename =filename.substring(filename.lastIndexOf(".")+1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), filename, null);
        String soundUser1 = webServer.getWebServerUrl() + storePath.getFullPath();
        //2.构建(上传)对象
        Fsr fsr = new Fsr();
        fsr.setId(UserHolder.getUserId());
        fsr.setSoundUrl(soundUser1);
        //3.调用API保存数据
       String blossom = blossomApi.save(fsr);
       if (StringUtils.isEmpty(blossom)){

       }
    }

    public PeachVo jieshou() {
        Fsr fsr =  blossomApi.get();

        Long id = fsr.getId();
        Jsr jsr = jsrApi.find(id);

        UserInfo userInfo = userInfoApi.findById(106L);

        PeachVo peachVo = new PeachVo();
        peachVo.setRemainingTimes(10000);
        peachVo.setNickname(userInfo.getNickname());
        peachVo.setGender(userInfo.getGender());
        peachVo.setAvatar(userInfo.getAvatar());
        peachVo.setSoundUrl(fsr.getSoundUrl());
        peachVo.setAge(userInfo.getAge());
        return peachVo;
    }

}

